package com.tjetc.adminmanage;


import com.tjetc.domian.Apartment;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.ApartmentService;
import com.tjetc.vo.Result;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

//  公寓管理
public class ApartmentManageView {
    private static Scanner scanner = new Scanner(System.in);
    private static ApartmentService apartmentService = ClientProxy.getProxyInstance(ApartmentService.class);

    public void start() {
        while (true) {
            System.out.println("公寓管理菜单");
            System.out.println("1.查询公寓");
            System.out.println("2.添加公寓");
            System.out.println("3.修改公寓");
            System.out.println("4.删除公寓");
            System.out.println("5.按面积查询");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    del();
                    break;
                case 5:
                    listByArea();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }

    private void listByArea() {
        System.out.println("按面积查询");
        System.out.println("输入面积");
        BigDecimal area = scanner.nextBigDecimal();
        List<Apartment> list = apartmentService.listByArea(area);
        if (list == null) {
            System.out.println("没有数据");
        } else {
            for (Apartment apartment : list) {
                System.out.println(apartment);
            }
        }
    }

    private void del() {
        System.out.println("删除公寓");
        System.out.println("输入公寓id");
        int id = scanner.nextInt();
        Result result = apartmentService.del(id);
        System.out.println(result.getMsg());
    }

    private void update() {
        System.out.println("修改公寓");
        System.out.println("输入公寓id");
        int apartmentid = scanner.nextInt();
        System.out.println("输入修改后的公寓楼号(X号楼)");
        String buildingnumber = scanner.next();
        System.out.println("输入修改后的单元号(X单元)");
        String unitnumber = scanner.next();
        System.out.println("输入修改后的房号");
        String roomnumber = scanner.next();
        System.out.println("输入修改后的面积");
        BigDecimal area = scanner.nextBigDecimal();
        Apartment apartment = new Apartment(apartmentid, buildingnumber, unitnumber, roomnumber, area);
        Result result = apartmentService.update(apartment);
        System.out.println(result.getMsg());

    }

    private void add() {
        System.out.println("添加公寓");
        System.out.println("输入公寓楼号(X号楼)");
        String buildingnumber = scanner.next();
        System.out.println("输入单元号(X单元)");
        String unitnumber = scanner.next();
        System.out.println("输入房号");
        String roomnumber = scanner.next();
        System.out.println("输入面积");
        BigDecimal area = scanner.nextBigDecimal();
        Apartment apartment = new Apartment(buildingnumber, unitnumber, roomnumber, area);
        Result result = apartmentService.add(apartment);
        System.out.println(result.getMsg());

    }

    private void list() {
        List<Apartment> list = apartmentService.list();
        if (list == null || list.isEmpty()) {
            System.out.println("没有数据");
        } else {
            for (Apartment apartment : list) {
                System.out.println(apartment);
            }
        }
    }
}
